package biolean;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

import javax.swing.*;

import biolean.files.BioFile;

public class FileForm extends JFrame implements Commons {
	private boolean isClosed = true;
	JButton openButton;
	private String filePath;
	private JFrame frame = this;
	private BioFile activeFile;

	/**
	 * @param args
	 */
	public FileForm(BioFile file) { 
		activeFile = file;
		filePath = activeFile.getFilePath();
		addWindowListener(new WinAdapter());
		setTitle("File ...");
		setSize(FILE_FORM_WIDTH, FILE_FORM_HEIGHT);
		
		setVisible(true);
		setResizable(true);
		isClosed = false;
		setLocationRelativeTo(null);


		setLayout(new BorderLayout());
		
		JPanel northPane = new JPanel();
		northPane.setLayout(new FlowLayout());
		add(northPane, BorderLayout.NORTH);
		JLabel label_n = new JLabel("File: ");
		northPane.add(label_n);
		
		final JTextField input = new JTextField(filePath, FILE_FORM_INPUT_COLS);
		northPane.add(input);
		
		JPanel southPane = new JPanel();
		southPane.setLayout(new FlowLayout());
		
		add(southPane, BorderLayout.SOUTH);
		
		final JButton b_ok = new JButton("OK");
		southPane.add(b_ok, BorderLayout.WEST);
		final JButton b_cancel = new JButton("Cancel");
		southPane.add(b_cancel, BorderLayout.NORTH);
		
		b_cancel.requestFocusInWindow();
		
		input.addFocusListener(new FocusListener() {
			
			@Override
			public void focusGained(FocusEvent e) {
				b_cancel.requestFocusInWindow();
				
				final JFileChooser fc;
				
				File f = new File(filePath);
				if(!f.exists()) {
					filePath = OUTPUT_DIR;
				}
					
				fc = new JFileChooser(filePath);
				
				int returnVal = fc.showOpenDialog(frame);

		        if (returnVal == JFileChooser.APPROVE_OPTION) {
		        	
		            File file = fc.getSelectedFile();
		            //This is where a real application would open the file.
		            filePath = file.toString();
		            input.setText(filePath);
		            b_ok.requestFocusInWindow();
		        }
		        else {
		        	b_cancel.requestFocusInWindow();
		        }
		        
			}

			@Override
			public void focusLost(FocusEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		
		b_ok.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				activeFile.setFilePath(input.getText());
				frame.dispose();
			}
		});
		
		b_cancel.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				frame.dispose();
			}
		});

	}

	public boolean isClosed() {
		return isClosed;
	}

	private class WinAdapter extends WindowAdapter {
		public void windowClosing(WindowEvent e) {
			isClosed = true;
		}

	}

}
